<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="3" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button @click="filter" icon="filter" style="margin-right: 8px; background: #f47920; color: white"
                >筛选</a-button
              >
            </span>
          </a-col>

          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol">
              <!-- <a-input-search ref="class" v-model="queryParam.jxbid" readOnly unselectable="on" @search="onSearchClass">
                <a-button slot="enterButton" :disabled="disabled">选择班级</a-button>
              </a-input-search> -->
              <j-dict-select-tag
                v-model="queryParam.id"
                placeholder="课程性质"
                dictCode="edu_dev_open.ve_jw_kcxz,xzmc,id,del_flag=0"
              ></j-dict-select-tag>
            </a-form-item>
          </a-col>

          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button @click="searchReset" icon="delete" style="margin-right: 8px; background: #f47920; color: white"
                >清除</a-button
              >
              <a-button type="primary" @click="searchQuery" icon="search">搜索</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">添加</a-button>

      <a-button @click="batchDel" type="primary" icon="delete">删除</a-button>
      <a-button @click="searchReset" type="primary" icon="reload">刷新</a-button>

      <a-button @click="recycle" type="danger" icon="delete">回收站</a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
        <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
        >项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{ fixed: true, selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        @change="handleTableChange"
      >
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">无此图片</span>
          <img
            v-else
            :src="getImgView(text)"
            height="25px"
            alt="图片不存在"
            style="max-width: 80px; font-size: 12px; font-style: italic"
          />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">无此文件</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="uploadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleDetail(record)">查看</a>
          <a-divider type="vertical" />
          <a @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical" />
          <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
            <a>删除</a>
          </a-popconfirm>
        </span>

        <span slot="isPublic" slot-scope="text, record">
          <a-switch
            default-checked
            @change="changeIsPublic(record.id, checked)"
            v-model="record.isPublic"
            :active-value="1"
            :inactive-value="0"
          />
        </span>

        <span slot="isCompulsory" slot-scope="text, record">
          <a-switch
            default-checked
            @change="changeIsCompulsory(record.id, checked)"
            v-model="record.isCompulsory"
            :active-value="1"
            :inactive-value="0"
          />
        </span>

        <span slot="isAble" slot-scope="text, record">
          <a-switch
            default-checked
            @change="changeIsAble(record.id, checked)"
            v-model="record.isAble"
            :active-value="1"
            :inactive-value="0"
          />
        </span>
      </a-table>
    </div>

    <veJwKcxz-modal ref="modalForm" @ok="modalFormOk"></veJwKcxz-modal>
    <ve-jw-kcxz-recycle-bin ref="recycle" @click="recycleFormOk"></ve-jw-kcxz-recycle-bin>
    <ve-jw-kcxz-filter ref="filter" @ok="filterFormOk"></ve-jw-kcxz-filter>
  </a-card>
</template>

<script>
import { EduListMixin } from '@/mixins/EduListMixin'
import VeJwKcxzModal from './modules/VeJwKcxzModal'
import VeJwKcxzRecycleBin from './modules/VeJwKcxzRecycleBin'
import VeJwKcxzFilter from './modules/VeJwKcxzFilter'
import { postAction, getAction } from '@/api/manage'

export default {
  name: 'VeJwKcxzList',
  mixins: [EduListMixin],
  components: {
    VeJwKcxzModal,
    VeJwKcxzRecycleBin,
    VeJwKcxzFilter,
  },
  data() {
    return {
      description: 've_jw_kcxz管理页面',
      // 表头
      columns: [
        {
          title: '课程性质',
          align: 'center',
          dataIndex: 'xzmc',
        },
        {
          title: '是否公共课',
          align: 'center',
          dataIndex: 'isPublic',
          scopedSlots: { customRender: 'isPublic' },
        },
        {
          title: '是否必修课',
          align: 'center',
          dataIndex: 'isCompulsory',
          scopedSlots: { customRender: 'isCompulsory' },
        },
        {
          title: '描述',
          align: 'center',
          dataIndex: 'description',
        },
        {
          title: '创建日期',
          align: 'center',
          dataIndex: 'createTime',
          customRender: function (text) {
            return !text ? '' : text.length > 10 ? text.substr(0, 10) : text
          },
        },

        {
          title: '最后修改时间',
          align: 'center',
          dataIndex: 'updateTime',
          customRender: function (text) {
            return !text ? '' : text.length > 10 ? text.substr(0, 10) : text
          },
        },
        // {
        //   title: '所属部门',
        //   align: 'center',
        //   dataIndex: 'sysOrgCode',
        // },
        {
          title: '是否有效',
          align: 'center',
          dataIndex: 'isAble',
          scopedSlots: { customRender: 'isAble' },
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          scopedSlots: { customRender: 'action' },
        },
      ],
      url: {
        list: '/jxgz/veJwKcxz/list',
        delete: '/jxgz/veJwKcxz/delete',
        deleteBatch: '/jxgz/veJwKcxz/deleteBatch',
        exportXlsUrl: '/jxgz/veJwKcxz/exportXls',
        importExcelUrl: 'jxgz/veJwKcxz/importExcel',
        updateIsPublic: '/jxgz/veJwKcxz/updateIsPublic',
        updateIsAble: '/jxgz/veJwKcxz/updateIsAble',
        updateIsCompulsory: '/jxgz/veJwKcxz/updateIsCompulsory',
      },
      dictOptions: {},
      isorter: [],
    }
  },

  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  methods: {
    initDictConfig() {},

    //回收站
    recycle: function () {
      this.$refs.recycle.show()
      this.$refs.recycle.title = '回收站'
      this.$refs.recycle.disableSubmit = false
    },

    //更新是否公共课
    changeIsPublic(id, checked) {
      this.loading = true
      postAction(this.url.updateIsPublic, { id: id })
        .then((res) => {
          if (res.success) {
            this.$message.success(res.message)
            this.loadData()
            this.onClearSelected()
          } else {
            this.$message.warning(res.message)
          }
        })
        .finally(() => {
          this.loading = false
        })
    },

    //更新是否必修
    changeIsCompulsory(id, checked) {
      this.loading = true
      postAction(this.url.updateIsCompulsory, { id: id })
        .then((res) => {
          if (res.success) {
            this.$message.success(res.message)
            this.loadData()
            this.onClearSelected()
          } else {
            this.$message.warning(res.message)
          }
        })
        .finally(() => {
          this.loading = false
        })
    },

    //更新是否有效
    changeIsAble(id, checked) {
      this.loading = true
      postAction(this.url.updateIsAble, { id: id })
        .then((res) => {
          if (res.success) {
            this.$message.success(res.message)
            this.loadData()
            this.onClearSelected()
          } else {
            this.$message.warning(res.message)
          }
        })
        .finally(() => {
          this.loading = false
        })
    },

    //筛选
    filter: function () {
      console.log('1111')
      this.$refs.filter.edit()
      this.$refs.filter.title = '筛选'
      this.$refs.filter.disableSubmit = false
    },

    //接收所筛选条件
    filterFormOk(xzmc, isCompulsory, isPublic, isAble, description, createBy) {
      //this.jcid=id

      this.queryParam = {}
      if (xzmc != null) {
        this.queryParam.xzmc = xzmc
      }
      if (isCompulsory != null) {
        this.queryParam.isCompulsory = isCompulsory
      }
      if (isPublic != null) {
        this.queryParam.isPublic = isPublic
      }
      if (isAble != null) {
        this.queryParam.isAble = isAble
      }
      if (description != null) {
        this.queryParam.description = description
      }
      if (createBy != null) {
        this.queryParam.createBy = createBy
      }

      this.loadData()
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>